﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Corsat.Library.DataManager;

namespace Corsat.Web.Administration
{
    public partial class AdjustCourseSerials : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                List<Library.Item> CourseNames = Corsat.Library.DataManager.Main.GetAllItemsByCategory(Library.DataManager.Category.CourseNames);
                CourseNames.Insert(0, new Library.Item());
                ddlCourseNames.DataSource = CourseNames;
                ddlCourseNames.DataTextField = "Value";
                ddlCourseNames.DataValueField = "Id";
                ddlCourseNames.DataBind();

                // Load Courses and store them in a datatable in the session object
                DataTable dtCourses = Main.GetAllCoursesWithDates();
                if (dtCourses != null && Session["dtCourses"] == null)
                    Session.Add("dtCourses", dtCourses);


                pnlAccountsInCourse.Visible = false;
                grdAccounts.DataSource = null;
                grdAccounts.DataBind();
            }
        }

        protected void ddlCourseNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Load Course Levels
            List<Library.Item> CourseLevels = Corsat.Library.DataManager.Main.GetCourseLevels(Convert.ToInt32(ddlCourseNames.SelectedValue));
            rdoLevel.DataSource = CourseLevels;
            rdoLevel.DataTextField = "Value";
            rdoLevel.DataValueField = "Id";
            rdoLevel.DataBind();

            pnlAccountsInCourse.Visible = false;
            grdAccounts.DataSource = null;
            grdAccounts.DataBind();

        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {
            // if No Course Title selected
            // OR (a Course Title is selected AND Course Has Levels but none is selected)
            // Clear Absence Grid
            if (ddlCourseNames.SelectedValue == "" || (ddlCourseNames.SelectedValue != "" && rdoLevel.Items.Count > 0 && rdoLevel.SelectedIndex == -1))
            {
                grdAccounts.DataSource = null;
                grdAccounts.DataBind();
                return;
            }


            // Get Course Level Id
            int courseLeveId = 0;
            if (rdoLevel != null && rdoLevel.SelectedValue != "")
                courseLeveId = Convert.ToInt32(rdoLevel.SelectedValue);

            // Get Course Id
            int courseId = GetCourseId(Convert.ToInt32(ddlCourseNames.SelectedValue), courseLeveId);

            // Get Absence Data
            DataTable dtAbsence = Main.GetCourseSerials(courseId, DateTime.Now.Year);

            // Set Grid Data Source and rebind
            pnlAccountsInCourse.Visible = true;
            grdAccounts.DataSource = dtAbsence;
            grdAccounts.DataBind();
        }

        /// <summary>
        /// Get Course Id
        /// </summary>
        /// <param name="CourseTitleId">Course Title Id </param>
        /// <param name="courseLevelId">Course Level Id</param>
        /// <returns></returns>
        private int GetCourseId(int CourseTitleId, int courseLevelId)
        {
            // if session doesn't have "dtCourses" datatable .. get all courses and store them in the session object
            if (Session["dtCourses"] == null)
            {
                // Get all Courses
                DataTable dtCourses = Main.GetAllCoursesWithDates();
                if (dtCourses == null)
                    return 0;

                // Store datatable in the session object
                if (dtCourses != null && Session["dtCourses"] == null)
                    Session.Add("dtCourses", dtCourses);
            }

            DataRow[] dr;
            // select Course Id from Session["dtCourses"] data Table
            if (courseLevelId == 0)
                dr = ((DataTable)Session["dtCourses"]).Select("CourseTitleId =" + CourseTitleId + " AND CourseLevelId IS NULL ");
            else
                dr = ((DataTable)Session["dtCourses"]).Select("CourseTitleId =" + CourseTitleId + " AND CourseLevelId =" + courseLevelId);

            if (dr.Length > 0)
                return (Convert.ToInt32(dr[0]["CourseId"]));
            else
                return 0;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<object, object>> AccountsSerials = new List<KeyValuePair<object, object>>();

            for (int i = 0; i < grdAccounts.Rows.Count; i++)
            {
                object AccountId = ((HiddenField)grdAccounts.Rows[i].FindControl("hdnAccountId")).Value;
                object AccountSerialInCourse = ((TextBox)grdAccounts.Rows[i].FindControl("txtAccountSerialInCourse")).Text;

                AccountsSerials.Add(new KeyValuePair<object, object>(AccountId, AccountSerialInCourse));
            }

            // Get Course Level Id
            int courseLeveId = 0;
            if (rdoLevel != null && rdoLevel.SelectedValue != "")
                courseLeveId = Convert.ToInt32(rdoLevel.SelectedValue);

            // Get Course Id
            int courseId = GetCourseId(Convert.ToInt32(ddlCourseNames.SelectedValue), courseLeveId);

            Library.DataManager.Main.AdjustSerials(courseId, DateTime.Now.Year, AccountsSerials);
        }

        protected void rdoLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAccountsInCourse.Visible = false;
            grdAccounts.DataSource = null;
            grdAccounts.DataBind();
        }
    }
}